<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      size="small"
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="上级分类" :disabled="categoryParentState">
        <el-input
          v-model="dataForm.parentName"
          :disabled="categoryParentState"
        ></el-input>
      </el-form-item>

      <el-form-item label="分类名称" prop="name">
        <el-input
          v-model="dataForm.name"
          autocomplete="off"
          placeholder="分类名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="图标" prop="icon">
        <el-input
          v-model="dataForm.icon"
          autocomplete="off"
          placeholder="图标"
        ></el-input>
      </el-form-item>
      <el-form-item label="计量单位" prop="productUnit">
        <el-input
          v-model="dataForm.productUnit"
          autocomplete="off"
          placeholder="计量单位"
        ></el-input>
      </el-form-item>
      <el-form-item label="排序号" prop="orderNum">
        <el-input-number
          v-model="dataForm.orderNum"
          controls-position="right"
          :min="0"
          label="排序号"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="显示状态" prop="dataState">
        <el-radio-group v-model="dataForm.dataState">
          <el-radio :label="0">显示</el-radio>
          <el-radio :label="-1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>


<script>
import {
  productCategoryFindById,
  addOrUpdateProductCategory,
} from "@/api/product";
import { treeDataTranslate } from "@/utils";

export default {
  data() {
    return {
      visible: false,
      categoryParentState: true,
      dataForm: {
        parentName: "顶级分类",
        name: "",
        parentId: 0,
        dataState: 0,
        orderNum: 0,
        productUnit: "",
        icon: "",
        id: "",
      },
      dataRule: {
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
        ],
        productUnit: [
          { required: true, message: "计量单位不能为空", trigger: "blur" },
        ],
      },
      dataListLoading: false,
    };
  },
  components: {},
  activated() {},
  methods: {
    init(data) {
      this.dataForm.id = 0;

      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        
        if (data.addChildren == 1) {
          this.dataForm.parentId = data ? data.id : 0;
          this.dataForm.parentName = data ? data.name : "";
          // 新增下级菜单
        } else if (data.addChildren == 2) {
          // 修改
          this.dataForm.id = data.id;
          this.dataForm.name = data.name;
          this.dataForm.parentId = data.parentId;
          this.dataForm.orderNum = data.orderNum;
          this.dataForm.productUnit = data.productUnit;
          this.dataForm.dataState = data.dataState;
          this.dataForm.icon = data.icon;
          productCategoryFindById({ id: this.dataForm.parentId }).then(
            (res) => {
              this.dataListLoading = true;
              if (res && res.code === 0) {
                this.dataForm.parentName = res.data.name;
              }
              this.dataListLoading = false;
            }
          );
        }
      });
    },
    // 菜单树选中
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          addOrUpdateProductCategory({
            id: this.dataForm.id || "",
            parentId: this.dataForm.parentId || 0,
            name: this.dataForm.name,
            orderNum: this.dataForm.orderNum,
            productUnit: this.dataForm.productUnit,
            dataState: this.dataForm.dataState,
            icon: this.dataForm.icon,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
